Option Strict On
Option Explicit On

'Copyright (c) 2007-2010, William Severance, Jr., WESNet Designs
'All rights reserved.

'Redistribution and use in source and binary forms, with or without modification, are permitted
'provided that the following conditions are met:

'Redistributions of source code must retain the above copyright notice, this list of conditions
'and the following disclaimer.

'Redistributions in binary form must reproduce the above copyright notice, this list of conditions
'and the following disclaimer in the documentation and/or other materials provided with the distribution.

'Neither the name of William Severance, Jr. or WESNet Designs may be used to endorse or promote
'products derived from this software without specific prior written permission.

'Disclaimer: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
'            OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
'            AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER BE LIABLE
'            FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
'            LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
'            INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
'            OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
'            IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

'Although I will try to answer questions regarding the installation and use of this software when
'such questions are submitted via e-mail to the below address, no promise of further
'support or enhancement is made nor should be assumed.

'Developer Contact Information:
'     William Severance, Jr.
'     WESNet Designs
'     679 Upper Ridge Road
'     Bridgton, ME 04009
'     Phone: 207-647-9375
'     E-Mail: bill@wesnetdesigns.com
'     Website: www.wesnetdesigns.com

Imports DotNetNuke
Imports DotNetNuke.Services.Exceptions
Imports System.Web.UI

Imports System.Xml
Imports System.IO

Namespace WESNet.DNN.Modules.EPrayer
    Partial Class Settings
        Inherits DotNetNuke.Entities.Modules.ModuleSettingsBase

#Region "Private Members"
        Private _MyConfiguration As Configuration
#End Region

#Region "Base Method Implementations"

        Public Overrides Sub LoadSettings()
            Dim li As ListItem
            Try
                If Not IsPostBack Then
                    _MyConfiguration = New Configuration(ModuleId, Settings)

                    With _MyConfiguration
                        cbEnableModeration.Checked = .EnableModeration
                        cbNewUsersAreTrusted.Checked = .NewUsersAreTrusted
                        cbAllowOwnPostEditing.Checked = .AllowOwnPostEditing
                        cbAllowOwnPostDeletion.Checked = .AllowOwnPostDeletion
                        li = ddlExpires.Items.FindByValue(.RequestLifespan.ToString)
                        If li Is Nothing Then
                            ddlExpires.SelectedIndex = 0
                        Else
                            ddlExpires.SelectedIndex = ddlExpires.Items.IndexOf(li)
                        End If
                        BindDistributions()
                        Utilities.DistributionCBLSet(cblAnonymousUsersViewableDistributions, .AnonymousUsersViewableDistributions)
                        Utilities.DistributionCBLSet(cblNewUsersViewableDistributions, .NewUsersViewableDistributions)
                        Utilities.DistributionCBLSet(cblNewPostDefaultDistributions, .NewPostDefaultDistributions)
                        tbPageSize.Text = .PageSize.ToString
                        tbMaxBodyLength.Text = .MaxBodyLength.ToString
                        cbEnableSubjectContact.Checked = .EnableSubjectContact
                        cbEnableSubjectNotification.Checked = .EnableSubjectNotification
                        cbEnableNotification.Checked = .EnableNotification
                        cbTrackVisits.Checked = .TrackVisits
                        cbTrackViews.Checked = .TrackViews
                        cbEnableDNNSearch.Checked = .EnableDNNSearch
                        cbUseAdminEmailAsSender.Checked = .UseAdminEmailAsSender
                        tbAdminEmail.Text = .AdminEmail
                        rblMailFormat.DataSource = [Enum].GetNames(GetType(DotNetNuke.Services.Mail.MailFormat))
                        rblMailFormat.DataBind()
                        rblMailFormat.SelectedValue = .MailFormat.ToString
                        rblMailSendMethod.DataSource = [Enum].GetNames(GetType(MailManager.MailSendMethods))
                        rblMailSendMethod.DataBind()
                        rblMailSendMethod.SelectedValue = .MailSendMethod.ToString
                        cbEnablePerPortalTemplates.Checked = .EnablePerPortalTemplates
                        If UserInfo.IsSuperUser Then
                            cbEnablePerPortalTemplates.Enabled = True
                            trEmailTemplates.Visible = True
                            trEMailTemplateEditor.Visible = True
                        Else
                            cbEnablePerPortalTemplates.Enabled = False
                            trEmailTemplates.Visible = .EnablePerPortalTemplates
                            trEMailTemplateEditor.Visible = .EnablePerPortalTemplates
                        End If
                        ctlEMailTemplateEditor.DefaultStyleName = .MailStyleName
                        ctlEMailTemplateEditor.MailFormat = .MailFormat
                        BindMailStyleNames()
                        li = ddlMailStyleName.Items.FindByText(.MailStyleName)
                        If Not li Is Nothing Then
                            ddlMailStyleName.SelectedIndex = ddlMailStyleName.Items.IndexOf(li)
                        End If
                    End With
                End If
            Catch exc As Exception           'Module failed to load
                ProcessModuleLoadException(Me, exc)
            End Try
        End Sub

        Public Overrides Sub UpdateSettings()
            Try
                _MyConfiguration = New Configuration(ModuleId, Settings)
                With _MyConfiguration
                    .EnableModeration = cbEnableModeration.Checked
                    .NewUsersAreTrusted = cbNewUsersAreTrusted.Checked
                    .AllowOwnPostEditing = cbAllowOwnPostEditing.Checked
                    .AllowOwnPostDeletion = cbAllowOwnPostDeletion.Checked
                    .RequestLifespan = Convert.ToInt32(ddlExpires.SelectedValue)
                    .AnonymousUsersViewableDistributions = Utilities.DistributionCBLGet(cblAnonymousUsersViewableDistributions, False)
                    .NewUsersViewableDistributions = Utilities.DistributionCBLGet(cblNewUsersViewableDistributions, False)
                    .NewPostDefaultDistributions = Utilities.DistributionCBLGet(cblNewPostDefaultDistributions, False)
                    .PageSize = Integer.Parse(tbPageSize.Text)
                    .MaxBodyLength = Integer.Parse(tbMaxBodyLength.Text)
                    .EnableSubjectContact = cbEnableSubjectContact.Checked
                    .EnableSubjectNotification = cbEnableSubjectNotification.Checked
                    .EnableNotification = cbEnableNotification.Checked
                    .TrackVisits = cbTrackVisits.Checked
                    .TrackViews = cbTrackViews.Checked
                    .EnableDNNSearch = cbEnableDNNSearch.Checked
                    .UseAdminEmailAsSender = cbUseAdminEmailAsSender.Checked
                    .AdminEmail = tbAdminEmail.Text
                    .MailStyleName = ddlMailStyleName.SelectedValue
                    .MailFormat = CType([Enum].Parse(GetType(DotNetNuke.Services.Mail.MailFormat), rblMailFormat.SelectedValue), DotNetNuke.Services.Mail.MailFormat)
                    .MailSendMethod = CType([Enum].Parse(GetType(MailManager.MailSendMethods), rblMailSendMethod.SelectedValue), MailManager.MailSendMethods)
                    .EnablePerPortalTemplates = cbEnablePerPortalTemplates.Checked
                End With
                _MyConfiguration.SaveSettings()
                ' refresh cache
                DotNetNuke.Entities.Modules.ModuleController.SynchronizeModule(ModuleId)
            Catch exc As Exception           'Module failed to load
                ProcessModuleLoadException(Me, exc)
            End Try
        End Sub

#End Region

#Region "Private Methods"

        Private Sub BindDistributions()
            Dim epc As New EPrayerController
            Dim Distributions As Generic.List(Of DistributionInfo) = epc.GetDistributions(ModuleId)
            With cblAnonymousUsersViewableDistributions
                .DataSource = Distributions
                .DataBind()
            End With
            With cblNewUsersViewableDistributions
                .DataSource = Distributions
                .DataBind()
            End With
            With cblNewPostDefaultDistributions
                .DataSource = Distributions
                .DataBind()
            End With
        End Sub

        Private Sub BindMailStyleNames()

            Dim TemplateFileName As String = ctlEMailTemplateEditor.TemplatePath & "EMail"
            Try
                Dim doc As XPath.XPathDocument = Utilities.OpenXMLTemplate(TemplateFileName, "")
                If Not doc Is Nothing Then
                    Dim xmlNav As XPath.XPathNavigator = doc.CreateNavigator()
                    Dim XPathExpression As String = "Templates/Template/Styles/Body[not(@Style=preceding::Body/@Style)]/@Style"

                    ' Get the iterator object for given Xpath search

                    Dim xmlNodeIterator As XPath.XPathNodeIterator = xmlNav.Select(XPathExpression)
                    ddlMailStyleName.Items.Clear()
                    If xmlNodeIterator.Count = 0 Then
                        ddlMailStyleName.Items.Add(Localization.GetString("Default", LocalResourceFile))
                    Else
                        For i As Integer = 0 To xmlNodeIterator.Count - 1
                            xmlNodeIterator.MoveNext()
                            ddlMailStyleName.Items.Add(xmlNodeIterator.Current.Value)
                        Next
                        ddlMailStyleName.Items.Insert(0, Localization.GetString("Please_Select", LocalResourceFile))
                    End If
                End If
            Catch exc As Exception
                DotNetNuke.UI.Skins.Skin.AddModuleMessage(Me, "Missing or invalid email template file " & TemplateFileName, Skins.Controls.ModuleMessage.ModuleMessageType.RedError)
            End Try
        End Sub
#End Region
    End Class

End Namespace

